//1:引入第三方模块
const express = require("express");
const mysql = require("mysql");
const cors = require("cors");
const session = require("express-session");
//2:配置第三方模块
 //2.1:配置连接池
 var pool = mysql.createPool({
   host:"127.0.0.1",
   user:"root",
   password:"",
   port:3306,
   database:"adidas",
   connectionLimit:15
 })
 //2.2:跨域
 var server = express();
 server.use(cors({
   origin:["http://127.0.0.1:8080",
   "http://localhost:8080"],
   credentials:true
 }))
 //2.3:session
 server.use(session({
   secret:"128位字符串",
   resave:true,
   saveUninitialized:true
 }))
 //2.9:指定静态目录
server.use(express.static("public"))

server.listen(3000);


 //首页商品
 server.get("/index",(req,res)=>{
   var sql="select pid,title,path,price,online from product where pid=1 or pid=3 or pid=5 or pid=6"
   pool.query(sql,(err,result)=>{
    if(err)throw err;
    var obj={code:1,mag:"查询成功",data:result};
      res.send(obj) 
    })
   })

//本格特惠全部
server.get("/all",(req,res)=>{
  var sql="select title,price,online,path,pid from product"
  pool.query(sql,(err,result)=>{
    if(err)throw err;
    var obj={code:1,mag:"查询成功",data:result};
      res.send(obj) 
  })
})


//SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC
//SELECT Company, OrderNumber FROM Orders ORDER BY Company
//本格特惠人气
server.get("/line",(req,res)=>{
  var sql="select title,price,online,path,pid from product order by online desc"
  pool.query(sql,(err,result)=>{
    if(err)throw err;
    var obj={code:1,mag:"查询成功",data:result};
      res.send(obj) 
  })
})

//本格特惠价格
server.get("/price",(req,res)=>{
  var sql="select title,price,online,path,pid from product order by price"
  pool.query(sql,(err,result)=>{
    if(err)throw err;
    var obj={code:1,mag:"查询成功",data:result};
      res.send(obj) 
  })
})

//鞋子分类
server.get("/fen",(req,res)=>{
  var sql="select fname,path,fid from fen"
  pool.query(sql,(err,result)=>{
    if(err)throw err;
    var obj={code:1,mag:"查询成功",data:result};
      res.send(obj) 
  })
})


//衣服分类
server.get("/yifu",(req,res)=>{
  var sql="select yname,path,yid from yifu"
  pool.query(sql,(err,result)=>{
    if(err)throw err;
    var obj={code:1,mag:"查询成功",data:result};
      res.send(obj) 
  })
})


//配件分类
server.get("/pei",(req,res)=>{
  var sql="select pname,path,jid from pei"
  pool.query(sql,(err,result)=>{
    if(err)throw err;
    var obj={code:1,mag:"查询成功",data:result};
      res.send(obj) 
  })
})